/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package cn.com.recloud.bbs.controller;


import cn.com.recloud.bbs.constant.MetaOptionConstant;
import cn.com.recloud.bbs.entity.bbs_article;
import cn.com.recloud.bbs.entity.metaoption;
import cn.com.recloud.bbs.service.ArticleService;
import cn.com.recloud.bbs.service.MetaoptionService;
import cn.com.recloud.bbs.util.FileUploadUtil;
import cn.com.recloud.bbs.vo.AppArticStatisticsVO;
import cn.com.recloud.bbs.vo.AppListArticVO;
import cn.com.recloud.core.util.Result;
import cn.com.recloud.one.spring.controller.BaseController;
import cn.com.recloud.one.spring.exception.OneException;
import cn.com.recloud.one.spring.util.RequestUserUtils;
import io.swagger.annotations.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author keblea xie
 * @date 2020-06-10
 */
@RestController
@RequestMapping("/api/bbs/article")
@Api(tags = "文章相关接口")
public class ArticleController extends BaseController<bbs_article, ArticleService> {
    @Resource
    ArticleService articleService;
    @Resource
    MetaoptionService metaoptionService;

    @Autowired
    private FileUploadUtil fileUploadUtil;


    @PostMapping("save")
    @ApiOperation("新增文章")
    public Result save(@ModelAttribute bbs_article bbs_article, @RequestParam("status") String status, @RequestParam(value = "file", required = false) MultipartFile file) {
        if (!Objects.isNull(file)) {
            bbs_article.setAvatar(fileUploadUtil.upload(file));
        }
        String articleId = articleService.saveArticle(bbs_article, status);
        return new Result(articleId);
    }

    @PostMapping("publishordispublish")
    @ApiOperation("详情页发布/取消发布文章")
    public Result publishOrPublish(@RequestBody bbs_article bbs_article) {
        if (StringUtils.isBlank(bbs_article.getBbs_articleid())) {
            return Result.failure("id不能为空");
        }
        String bbsArticleStatus = bbs_article.getBbs_article_status();
        if (StringUtils.isBlank(bbsArticleStatus)) {
            return Result.failure("文章状态不能为空");
        }
        bbs_article bbsArticle = new bbs_article();
        metaoption metaoption = metaoptionService.findMetaOptionBySetNameAndValue(MetaOptionConstant.ARTICLE_STATUS, bbsArticleStatus);
        bbsArticle.setBbs_articleid(bbs_article.getBbs_articleid());
        bbsArticle.setBbs_article_status(metaoption.getValue());
        bbsArticle.setBbs_article_statusname(metaoption.getName());
        bbsArticle.setReleasedon(null);
        bbsArticle.setReleasedby(null);
        bbsArticle.setReleasedbyname(null);
        //2发布,1草稿
        if ("2".equals(bbsArticleStatus)) {
            bbsArticle.setReleasedon(new Date());
            bbsArticle.setReleasedby(RequestUserUtils.getUserId());
            bbsArticle.setReleasedbyname(RequestUserUtils.getUserName());
        }
        articleService.updateById(bbsArticle);
        return Result.SUCCESS;
    }

    @PostMapping("multipublishcancel/{type}")
    @ApiOperation("列表页批量发布/取消发布")
    public Result multiPublishCancel(@PathVariable("type") String type, @RequestBody List<String> idList) {
        if (StringUtils.isBlank(type)) {
            throw new OneException("发布状态不能为空");
        }
        if (CollectionUtils.isEmpty(idList)) {
            throw new OneException("至少选择一条记录");
        }
        articleService.publishOrCancel(idList, type);
        return Result.SUCCESS;
    }

    @PostMapping("modify/{flag}")
    @ApiOperation("更新文章")
    public Result modify(@PathVariable("flag") String flag, @ModelAttribute bbs_article bbs_article, @RequestParam("status") String status, @RequestParam(value = "file", required = false) MultipartFile file) {
        if (!Objects.isNull(file)) {
            bbs_article.setAvatar(fileUploadUtil.upload(file));
        }

        if (StringUtils.isBlank(status)) {
            throw new OneException("发布状态不能为空");
        }
        String articleId = articleService.updateArticle(bbs_article, status, flag);
        return new Result(articleId);
    }

    @GetMapping("/listarticle")
    @ApiOperation("App获取列表页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeId", value = "文章的分类id"),
            @ApiImplicitParam(name = "pageSize", value = "一页显示的条数")
    })
    public Result ApplistArticle(@ApiParam(name = "typeId", value = "文章的分类id") String typeId, Integer pageIndex, Integer pageSize) {
        List<AppListArticVO> list = articleService.listArticle(typeId, pageIndex, pageSize);
        return new Result(list);
    }

    @GetMapping("getstatistics/{id}")
    @ApiOperation("app文章详情页获取统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章id")
    })
    public Result getAppArticleStatistics(@PathVariable("id") String id) {
        AppArticStatisticsVO vo = articleService.getAppArticleStatistics(id);
        return new Result(vo);
    }
}

